package repository.beans;

import java.util.ArrayList;
import java.util.List;

import javax.ejb.Stateless;

import model.Candidate;
import model.Comment;
import model.Position;

import org.jboss.logging.annotations.Pos;

import repository.PositionRepository;
import repository.exceptions.RepositoryException;

@Stateless(name = "PositionRepository", mappedName = "ejb/PositionRepository")
public class PositionRepositoryBean extends
		BaseRepositoryBean<Position, Long> implements PositionRepository {

	public PositionRepositoryBean() {
		super(Position.class);
	}

	public List<Candidate> getCandidatesForPosition(Long id)
			throws RepositoryException {

		Position p = this.getById(id);
		return p.getCandidates();

	}

	public List<Comment> getCommentsForPosition(Long id)
			throws RepositoryException {
		Position p = this.getById(id);
		return p.getComments();
	}   
	
	public void approvePosition(Long id) throws RepositoryException{
		
		Position p=this.getById(id);
		p.setActive(true);
		this.merge(p);
		
		
	}
	
	public void deactivatePosition(Long id) throws RepositoryException{
		Position p=this.getById(id);
		p.setActive(false);
		this.merge(p);
	}
	
	public void addCommentToPosition(Long id, Comment comment) throws RepositoryException{
		Position p=this.getById(id);
		List<Comment> commments = p.getComments();
		commments.add(comment);
		p.setComments(commments);
		this.merge(p);
	}
	
	public void selectCandidate(Long id,Candidate c) throws RepositoryException{
		Position p=this.getById(id);
		if(p.getTotalPlaces()>p.getAcceptedCandidates().size()){
			List<Candidate> candidates=p.getCandidates();
			candidates.remove(c);
			p.setCandidates(candidates);
			List<Candidate> accepted=p.getAcceptedCandidates();
			accepted.add(c);
			p.setAcceptedCandidates(accepted);
			int i=p.getTotalPlaces();
			i--;
			p.setTotalPlaces(i);
		}
		else{
			p.setActive(false);
		}
		this.merge(p);
	}
	
	public List<Position> getActivePositions() throws RepositoryException{
		List<Position> all = new ArrayList<Position>();
		for(Position p:this.getAll()){
			if(p.isActive()){
				all.add(p);
			}
		}
		return all;
	}
	
	public List<Position> getInactivePositions() throws RepositoryException{
		List<Position> all = new ArrayList<Position>();
		for(Position p:this.getAll()){
			if(!p.isActive()){
				all.add(p);
			}
		}
		return all;
	}
	
	public Position getPositionByName(String name) throws RepositoryException {
		for(Position p:this.getAll()){
			if(p.getDepartment().getName().equals(name)){
				return p;
			}
		}
		return null;
	}
	
	public List<Position> getApprovedPositions() throws RepositoryException{
		List<Position> all = new ArrayList<Position>();
		for(Position p:this.getAll()){
			if(p.isApproved()){
				all.add(p);
			}
		}
		return all;
	}

	@Override
	public List<Position> getUnApprovedPositions() throws RepositoryException {
		List<Position> all = new ArrayList<Position>();
		for(Position p:this.getAll()){
			if(!p.isApproved()){
				all.add(p);
			}
		}
		return all;
	}
	

}
